<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script type="text/javascript" src="../js/util.js"></script>
</head>

<body>
    <div>
        请选择插入点(单元格):
        <input type="text" name="" id="rangeAddress" value="" disabled=false>
        <br>
        跳转到指定的单元格:
        <input type="text" name="" id="linkTo" value="">

        <button id="addSummaryHere" onclick="addSummaryHereOnClick()" style="width: 100%;margin: 1em 0em;">插入</button>

        <fieldset>
            <legend>说明</legend>
            <ul>
                <li>默认插入点为当前工作表 A1 单元格;</li>
                <li>默认竖着插入;</li>
                <li>默认使用超链接(HYPERLINKS)跳转（<i>非公式</i>）;</li>
                <li>默认跳转到工作表的 A1 单元格。</li>
                <li>插入之前会先删除已有超链，如果前后工作表数量有变动，最好先自己手动清除再点"插入"。</li>
            </ul>
        </fieldset>
        <p id="foo"></p>
    </div>
    <style>
        div {
            padding: 1em;
            margin: 0%;
        }
    </style>

    <script type="text/javaScript">
        document.getElementById("rangeAddress").setAttribute("value",wps.Application.ActiveCell.Address());

        wps.ApiEvent.AddApiEventListener("SheetSelectionChange",()=>{
            document.getElementById("rangeAddress").setAttribute("value",wps.Application.ActiveCell.Address())
        });
        function addSummaryHereOnClick(){
            let visibleSheets = getShs().visible;
            let pointAddress = document.getElementById("rangeAddress").attributes.getNamedItem("value").value
            let acCell = wps.Application.ActiveCell;
            let point;
            if(acCell.Address()!==pointAddress){
                point = wps.Application.ActiveSheet.Range("A1");
            }else{
                point = acCell
            }
            let linkTo = document.getElementById("linkTo").value||"A1";
            document.getElementById('foo').textContent =linkTo
            for(let i =0;i<visibleSheets.length;i++){
                let inputHere = point.Offset(i,0);
                inputHere.Hyperlinks.Delete();// 先删除；
                inputHere.Hyperlinks.Add(inputHere, "", `${visibleSheets[i]}!${linkTo}`, "", `${visibleSheets[i]}`)
            }
        }
    </script>
</body>

</html>